package org.example.model;

import org.example.promotion.PromotionRule;
import org.example.promotion.ThresholdDiscountPromotion;

import java.util.ArrayList;
import java.util.List;

public class ShoppingCart {
    private final List<FruitItem> items = new ArrayList<>();
    private final List<PromotionRule> promotionRules = new ArrayList<>();

    public void addFruit(Fruit fruit, int weight) {
        if (weight < 0) {
            throw new IllegalArgumentException("重量不能为负数");
        }
        items.add(new FruitItem(fruit, weight));
    }

    public void addPromotionRule(PromotionRule rule) {
        promotionRules.add(rule);
    }

    public double calculateTotal() {
        double total = 0;
        for (FruitItem item : items) {
            total += item.calculatePrice();
        }

        // 应用所有促销规则，保留最优折扣
        double discountedTotal = total;
        for (PromotionRule rule : promotionRules) {
            double newTotal = rule.applyPromotion(total);
            if (newTotal < discountedTotal) {
                discountedTotal = newTotal;
            }
        }

        return discountedTotal;
    }
}
